package cn.iocoder.yudao.module.lfd.service.shop;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.lfd.controller.admin.shop.vo.ShopCreateReqVO;
import cn.iocoder.yudao.module.lfd.controller.admin.shop.vo.ShopExportReqVO;
import cn.iocoder.yudao.module.lfd.controller.admin.shop.vo.ShopPageReqVO;
import cn.iocoder.yudao.module.lfd.controller.admin.shop.vo.ShopUpdateReqVO;
import cn.iocoder.yudao.module.lfd.convert.shop.ShopConvert;
import cn.iocoder.yudao.module.lfd.dal.dataobject.shop.LfdShopDO;
import cn.iocoder.yudao.module.lfd.dal.mysql.shop.LfdShopMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.lfd.enums.ErrorCodeConstants.SHOP_NOT_EXISTS;

/**
 * 店铺 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class ShopService  {

    @Resource
    private LfdShopMapper lfdShopMapper;


    public Long createShop(ShopCreateReqVO createReqVO) {
        // 插入
        LfdShopDO shop = ShopConvert.INSTANCE.convert(createReqVO);
        lfdShopMapper.insert(shop);
        // 返回
        return shop.getId();
    }


    public void updateShop(ShopUpdateReqVO updateReqVO) {
        // 校验存在
        validateShopExists(updateReqVO.getId());
        // 更新
        LfdShopDO updateObj = ShopConvert.INSTANCE.convert(updateReqVO);
        lfdShopMapper.updateById(updateObj);
    }


    public void deleteShop(Long id) {
        // 校验存在
        validateShopExists(id);
        // 删除
        lfdShopMapper.deleteById(id);
    }

    private void validateShopExists(Long id) {
        if (lfdShopMapper.selectById(id) == null) {
            throw exception(SHOP_NOT_EXISTS);
        }
    }


    public LfdShopDO getShop(Long id) {
        return lfdShopMapper.selectById(id);
    }


    public List<LfdShopDO> getShopList(Collection<Long> ids) {
        return lfdShopMapper.selectBatchIds(ids);
    }


    public PageResult<LfdShopDO> getShopPage(ShopPageReqVO pageReqVO) {
        return lfdShopMapper.selectPage(pageReqVO);
    }


    public List<LfdShopDO> getShopList(ShopExportReqVO exportReqVO) {
        return lfdShopMapper.selectList(exportReqVO);
    }


    public List<LfdShopDO> getAll() {
        return lfdShopMapper.selectList();
    }

}
